package com.ruoyi.system.service.h5.impl;

import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.system.domain.MiniSelfSelection;
import com.ruoyi.system.domain.MiniStock;
import com.ruoyi.system.domain.MiniSubject;
import com.ruoyi.system.domain.vo.h5.SelfSelectionListVO;
import com.ruoyi.system.mapper.MiniSelfSelectionMapper;
import com.ruoyi.system.service.IMiniStockService;
import com.ruoyi.system.service.IMiniSubjectService;
import com.ruoyi.system.service.h5.MiniSelfSelectionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.sql.Timestamp;
import java.time.LocalDateTime;
import java.util.List;

/**
* @author ld
* @description 针对表【mini_self_selection(自选表)】的数据库操作Service实现
* @createDate 2023-11-19 17:39:49
*/
@Service
public class MiniSelfSelectionServiceImpl extends ServiceImpl<MiniSelfSelectionMapper, MiniSelfSelection>
    implements MiniSelfSelectionService{

    @Autowired
    private IMiniSubjectService miniSubjectService;
    @Autowired
    private IMiniStockService miniStockService;
    /**
     * 添加自选
     *
     * @param selfSelection
     * @return
     */
    @Override
    public AjaxResult add(MiniSelfSelection selfSelection) {
        AjaxResult check = checkAddSelection(selfSelection);
        if (check.isError()){
            return check;
        }
        selfSelection.setCreateTime(Timestamp.valueOf(LocalDateTime.now()));
        selfSelection.setCreateBy(selfSelection.getOpenid());
        if ("2".equals(selfSelection.getType())){
            MiniSubject subject = miniSubjectService.getById(selfSelection.getBusinessId());
            selfSelection.setBusinessName(subject.getName());
            /*if (subject.getParentId()!=null) {
                MiniSubject parentSubject = miniSubjectService.getById(subject.getParentId());
                selfSelection.setBusinessName(parentSubject.getName()+"-"+subject.getName());
            }*/
        }else if ("1".equals(selfSelection.getType())){
            MiniStock stock = miniStockService.getOne(Wrappers.<MiniStock>lambdaQuery().eq(MiniStock::getStockNo,selfSelection.getStockNo()));
            selfSelection.setBusinessName(stock.getName());
            selfSelection.setBusinessId(stock.getId());
        }
        this.save(selfSelection);
        return AjaxResult.success();
    }

    /**
     * 自选列表
     *
     * @param type
     * @param openid
     * @return
     */
    @Override
    public AjaxResult selectionList(String type, String openid) {
        AjaxResult check = checkListSelection(type,openid);
        if (check.isError()) {
            return check;
        }
        List<SelfSelectionListVO> result;
        if ("1".equals(type)){
            result = this.getBaseMapper().selectSelfSelectionWithStock(type,openid);
        }else{
            result = this.getBaseMapper().selectSelfSelectionWithSubject(type,openid);
        }
        return AjaxResult.success(result);
    }

    /**
     * 取消自选
     *
     * @param id
     * @param openid
     * @param type
     * @param stockNo
     * @return
     */
    @Override
    public AjaxResult disabled(Long id, String openid, String type, String stockNo) {
        AjaxResult check = checkDisabledSelection(id, openid, type, stockNo);
        if (check.isError()) {
            return check;
        }
        LambdaUpdateWrapper<MiniSelfSelection> updateWrapper = Wrappers.<MiniSelfSelection>lambdaUpdate().set(MiniSelfSelection::getDeleted, 1)
                .eq(MiniSelfSelection::getOpenid, openid)
                .eq(MiniSelfSelection::getType, type);
        if ("2".equals(type)){
            updateWrapper.eq(MiniSelfSelection::getBusinessId,id);
        }
        if ("1".equals(type)){
            updateWrapper.eq(MiniSelfSelection::getStockNo,stockNo);
        }
        this.update(updateWrapper);
        return AjaxResult.success();
    }
    /**
     * 以openid查询自选数量
     * @param openid
     * @return
     */
    @Override
    public List<MiniSelfSelection> selectSelfSelectionCountByOpenid(String openid) {
        return this.getBaseMapper().selectSelfSelectionCountByOpenid(openid);
    }

    /**
     * 检查是否添加到自选了
     *
     * @param type
     * @param id
     * @param stockNo
     * @return
     */
    @Override
    public AjaxResult<Integer> check(String type, String openid, Long id, String stockNo) {
        Integer integerAjaxResult = this.getBaseMapper().checkSelfSelection(type, openid, id,stockNo);
        if (integerAjaxResult == null) {
            return AjaxResult.success("1");
        }
        return AjaxResult.error("0");
    }

    private AjaxResult checkDisabledSelection(Long id, String openid, String type, String stockNo) {
        if (StringUtils.isBlank(openid)) {
            return AjaxResult.error("openid不能为空");
        }
        if (StringUtils.isBlank(type)) {
            return AjaxResult.error("type必传，以区分是题材还是股票");
        }
        if (id == null && "2".equals(type)) {
            return AjaxResult.error("取消题材自选必须传id");
        }
        if (StringUtils.isBlank(stockNo) && "1".equals(type)) {
            return AjaxResult.error("取消股票自选必须传stockNo");
        }
        return AjaxResult.success();
    }

    private AjaxResult checkListSelection(String type, String openid) {
        if (StringUtils.isBlank(openid)) {
            return AjaxResult.error("openid不能为空");
        }
        if (StringUtils.isBlank(type)) {
            return AjaxResult.error("自选类型不能为空");
        }
        if (!"1".equals(type)&& !"2".equals(type)) {
            return AjaxResult.error("出现意外的类型");
        }
        return AjaxResult.success();
    }

    private AjaxResult checkAddSelection(MiniSelfSelection selfSelection) {
        if (StringUtils.isBlank(selfSelection.getOpenid())) {
            return AjaxResult.error("openid不能为空");
        }
        if (StringUtils.isBlank(selfSelection.getType())) {
            return AjaxResult.error("自选类型不能为空");
        }
        if (!"1".equals(selfSelection.getType())&& !"2".equals(selfSelection.getType())) {
            return AjaxResult.error("出现意外的类型");
        }
        if (selfSelection.getBusinessId() == null && "2".equals(selfSelection.getType())) {
            return AjaxResult.error("添加题材自选必须传id");
        }
        if (StringUtils.isBlank(selfSelection.getStockNo()) && "1".equals(selfSelection.getType())) {
            return AjaxResult.error("添加股票自选必须传stockNo");
        }
        if (this.check(selfSelection.getType(), selfSelection.getOpenid(), selfSelection.getBusinessId(),selfSelection.getStockNo()).isError()){
            return AjaxResult.error("已经添加到自选");
        }
        if ("1".equals(selfSelection.getType())&& StringUtils.isBlank(selfSelection.getStockNo())) {
            return AjaxResult.error("自选股票的时候必须传股票代码");
        }
        return AjaxResult.success();
    }
}




